

package com.springcloud.sc.ucpv5.client.commondata;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashSet;

/**
 * Comments : 操作结果对象，配合SecuResultDlg使用， 基中的数据会在SecuResultDlg的详细信息表格中显示 7-5
 */

public class SecuResult {
	public final static int RET_SUCCEED = 0;
	public final static int RET_TIME_OUT = -1;
	public final static int RET_FAIL = 2;
	public final static int RET_PART_SUCCEED = 3;
	public final static int RET_OFFLINE_DATA = 4;

	public final static int TEXT_FIELD_OPER = 1;
	public final static int TEXT_FIELD_OBJ = 2;
	public final static int TEXT_FIELD_ERROR = 3;
	// 操作总数
	private int operCount = 0;

	private ArrayList<Result> errorList = new ArrayList<Result>();
	private ArrayList<Result> succList = new ArrayList<Result>();
	private HashSet<Integer> errorCodeMap = new HashSet<Integer>();

	private static HashSet<Integer> filterSuccMsgCmdIDSet = new HashSet<Integer>();

	private static String localIp = "";
	static {
		try {
			localIp = InetAddress.getLocalHost().getHostAddress();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 添加不在消息栏中打印成功信息的命令字
	 * 
	 * @date 2006-9-30
	 * @param cmdId
	 * @see
	 */
	public static void notPrintSuccMsg(int cmdId) {
		filterSuccMsgCmdIDSet.add(new Integer(cmdId));
	}

	public SecuResult() {
	}

	/**
	 * 添加操作结果
	 * 
	 * @date 2006-7-7
	 * @param obj
	 * @param cmdID
	 * @param retCode
	 * @param isLogToServer
	 * @see
	 */
	public void addResult(Object obj, int type, int cmdID, int retCode, boolean isLogToServer) {
		Object resultObj = obj;

		if (retCode != SecuResult.RET_SUCCEED) {
			addError(resultObj, type, cmdID, retCode);

			errorCodeMap.add(new Integer(retCode));
		} else {
			addSucced(resultObj, type, cmdID);
		}
	}

	public void addResult(Object obj, int type, int cmdID, int retCode) {
		try {
			addResult(obj, type, cmdID, retCode, true);
		} catch (Throwable exp) {
		}
	}

	public void addError(Object obj, String cmdName, String errMsg) {
		Result resultObj = new Result();
		resultObj.objName = (null == obj) ? "" : obj.toString();
		resultObj.msg = errMsg;
		resultObj.operName = cmdName == null ? "" : String.format(cmdName, new Object[] { "" });

		String operFormatName = "";
		if (cmdName != null) {
			operFormatName = String.format(cmdName, new Object[] { obj });
		}

		operCount++;
		errorList.add(resultObj);
	}

	/**
	 * 添加错误码记录
	 * 
	 * @date 2006-7-5
	 * @param obj
	 *            :错误发生的对象
	 * @param cmdID
	 *            :操作命令字
	 * @param errCode
	 *            :错误码
	 * @see
	 */
	private void addError(Object obj, int type, int cmdID, int errCode) {
		Result resultObj = new Result();
		resultObj.objName = (null == obj) ? "" : obj.toString();
		resultObj.msg = "";
		// OperItem operItem = CmdMgr.getInstance().getOperItem(type,cmdID);

		operCount++;
		errorList.add(resultObj);
	}

	/**
	 * 添加操作成功记录
	 * 
	 * @date 2006-7-7
	 * @param obj
	 * @param cmdId
	 * @see
	 */
	private void addSucced(Object obj, int type, int cmdId) {
		Result resultObj = new Result();
		resultObj.objName = (null == obj) ? "" : obj.toString();
		String operFormatName = "";
		operCount++;
		succList.add(resultObj);
	}

	/**
	 * 合并操作结果
	 * 
	 * @date 2006-7-7
	 * @param result
	 * @see
	 */
	public void uniteResult(SecuResult result) {
		if (null == result || result == this) {
			return;
		}

		errorList.addAll(result.errorList);
		succList.addAll(result.succList);
		errorCodeMap.addAll(result.errorCodeMap);
		this.operCount = errorList.size() + succList.size();

		result.clear();
	}

	/**
	 * 获得返回结果
	 * 
	 * @date 2006-7-7
	 * @return
	 * @see
	 */
	public int getRetCode() {
		if (errorCodeMap.size() == 1) {
			return SecuResult.RET_OFFLINE_DATA;
		} else if (operCount == succList.size()) {
			return SecuResult.RET_SUCCEED;
		} else if (operCount == errorList.size()) {
			return SecuResult.RET_FAIL;
		} else {
			return SecuResult.RET_PART_SUCCEED;
		}
	}

	/**
	 * 是否有操作成功
	 * 
	 * @date 2006-7-12
	 * @return
	 * @see
	 */
	public boolean hasSomeOperSucc() {
		int retCode = getRetCode();
		return (retCode == SecuResult.RET_SUCCEED) || (retCode == SecuResult.RET_PART_SUCCEED);
	}

	public boolean containErrorCode(int errorCode) {
		return errorCodeMap.contains(new Integer(errorCode));
	}

	/**
	 * 清空错误码列表中的数据
	 * 
	 * @date 2006-7-5
	 * @see
	 */
	public void clear() {
		errorList.clear();
		succList.clear();
		errorCodeMap.clear();
	}

	private static class Result {
		private String operName;
		private String objName;
		private String msg;

		public String getFieldText(int fieldId) {
			switch (fieldId) {
			case TEXT_FIELD_OPER:
				return operName;
			case TEXT_FIELD_OBJ:
				return objName;
			case TEXT_FIELD_ERROR:
				return msg;
			default:
				return null;
			}
		}

		public int getIdField(int fieldId) {
			return -1;
		}

	}
}
